package org.yang.faq.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yang.faq.common.Message;
import org.yang.faq.model.Comment;
import org.yang.faq.model.Praise;
import org.yang.faq.model.Program;
import org.yang.faq.service.CommentService;
import org.yang.faq.service.PraiseService;
import org.yang.faq.service.ProgramService;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 微信端口
 */
@RestController
public class WxChatController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ProgramService programService;
    @Autowired
    private PraiseService praiseService;

    @ApiOperation("获取所有的节目清单")
    @RequestMapping(value = "listProgrom", method = RequestMethod.GET)
    public List<Program> listProgrom() {
        return programService.selectList(new EntityWrapper<Program>());
    }

    @ApiOperation("添加评论")
    @ApiImplicitParam(name = "comment", value = "评论实体", dataType = "Comment")
    @RequestMapping(value = "addComment", method = RequestMethod.POST)
    public Message addComment(@RequestBody Comment comment) {
        comment.setCreateTime(new Date());
        Message message = new Message();
        sendMessage(message, commentService.insert(comment));
        return message;
    }

    @ApiOperation("添加点赞")
    @ApiImplicitParam(name = "praise", value = "点赞实体", dataType = "Praise")
    @RequestMapping(value = "addPraise", method = RequestMethod.POST)
    public Message addPraise(@RequestBody Praise praise) {
        praise.setCreateTime(new Date());
        Message message = new Message();
        sendMessage(message, praiseService.insert(praise));
        return message;
    }

    @ApiOperation("获取点赞")
    @ApiImplicitParam(name = "pid", value = "节目ID", dataType = "String")
    @RequestMapping(value = "listPraise/{pid}", method = RequestMethod.GET)
    public List<Praise> listPraise(@PathVariable("pid") String pid) {
        return praiseService.selectList(new EntityWrapper<Praise>().where("p_id", pid));
    }

    private void sendMessage(Message message, boolean insert) {
        if (insert) {
            message.setState(true);
            message.setMsg("添加成功");
        } else {
            message.setState(false);
            message.setMsg("添加失败");
        }
    }
}
